package mipushdemo.xiaomi.com.diyitonghang.utils;

import android.os.Environment;
import android.util.Log;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import mipushdemo.xiaomi.com.diyitonghang.api.ApiService;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtils {
    private static RetrofitUtils retrofit;

    // 构造函数私有化
    private RetrofitUtils() {
    }

    // 提供一个全局的静态方法
    public static RetrofitUtils newInstance() {
        if (retrofit == null) {
            synchronized (RetrofitUtils.class) {
                if (retrofit == null) {
                    retrofit = new RetrofitUtils();
                }
            }
        }
        return retrofit;
    }



//    public static Interceptor provideOfflineCacheInterceptor ()
//    {
//        return new Interceptor()
//        {
//            @Override
//            public Response intercept (Chain chain) throws IOException
//            {
//                Request request = chain.request();
//
//                /**
//                 * 未联网获取缓存数据
//                 */
//                if (!HttpNetUtil.isNetworkConnected() )
//                {
//                    //在20秒缓存有效，此处测试用，实际根据需求设置具体缓存有效时间
//                    CacheControl cacheControl = new CacheControl.Builder()
//                            .maxStale(20, TimeUnit.SECONDS )
//                            .build();
//
//                    request = request.newBuilder()
//                            .cacheControl( cacheControl )
//                            .build();
//                }
//
//                return chain.proceed( request );
//            }
//        };
//    }
//
//    public static Interceptor provideCacheInterceptor ()
//    {
//        return new Interceptor()
//        {
//            @Override
//            public Response intercept (Chain chain) throws IOException
//            {
//                Response response = chain.proceed( chain.request() );
//
//                // re-write response header to force use of cache
//                // 正常访问同一请求接口（多次访问同一接口），给30秒缓存，超过时间重新发送请求，否则取缓存数据
//                CacheControl cacheControl = new CacheControl.Builder()
//                        .maxAge(30, TimeUnit.SECONDS )
//                        .build();
//
//                return response.newBuilder()
//                        .header( "cacheControl", cacheControl.toString() )
//                        .build();
//            }
//        };
//    }
//
//    //设置缓存目录和缓存空间大小
//    private static Cache provideCache ()
//    {
//        Cache cache = null;
//        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mCache-net";
//        try
//        {
//            cache = new Cache( new File( path, "http-cache" ),
//                    10 * 1024 * 1024 ); // 10 MB
//        }
//        catch (Exception e)
//        {
//            Log.e("cache","Could not create Cache!");
//        }
//        return cache;
//    }
//
//    private static OkHttpClient provideOkHttpClient ()
//    {
//        //日志
//        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
//        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//        return new OkHttpClient.Builder()
//                .addInterceptor( interceptor )
//                .addInterceptor( provideOfflineCacheInterceptor() )
//                .addNetworkInterceptor( provideCacheInterceptor() )
//                .cache( provideCache() )
//                .build();
//    }

    public ApiService netWorkRequest(){
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://39.107.224.233/firstga/app/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService;
    }
}


